import pygame
import time
from types import SimpleNamespace
from common import (
    white,
    black,
    red,
    green,
    blue,
    font_path,
    pink,
    go_next_level,
    get_score,
    add_score,
)

screen_width, screen_height = 1000, 500
circuit_image = pygame.image.load("./assets/circuit_3.png")
fire_image = pygame.image.load("./assets/fire.jpg")
button_text_font = pygame.font.Font(font_path, 20)
title_text_font = pygame.font.Font(font_path, 30)

button_plus = SimpleNamespace(
    rect=pygame.Rect(150, 400, 150, 50),
    color=pink,
    text="+电源电压增加1",
    font_size=20,
    font_color=black,
)
button_number = SimpleNamespace(
    rect=pygame.Rect(350, 400, 150, 50),
    color=white,
    text="当前电压：0V",
    font_size=20,
    font_color=black,
)
button_minus = SimpleNamespace(
    rect=pygame.Rect(550, 400, 150, 50),
    color=green,
    text="-电源电压减少1",
    font_size=20,
    font_color=black,
)
button_submit = SimpleNamespace(
    rect=pygame.Rect(750, 400, 100, 50),
    color=blue,
    text="提交",
    font_size=20,
    font_color=black,
)
light_circle = SimpleNamespace(
    center_x=525,
    center_y=168,
    radius=20,
    color=red,
)


buttons = [button_plus, button_number, button_minus, button_submit]

score = 0
voltage = 0
fire = False
fire_time = -1

def draw(screen):
    global score, voltage, fire, fire_time
    # 绘制背景
    screen.fill(white)

    # 绘制title
    font = title_text_font
    text = font.render(
        f"当前得分（{get_score()}）关卡3：选择正确的电源电压",
        True,
        black,
    )
    text_rect = text.get_rect(center=(screen_width // 2, 50))
    screen.blit(text, text_rect)

    # 绘制电路板
    circuit = pygame.transform.smoothscale(circuit_image, (1000, 250))
    screen.blit(circuit, (0, 100))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        elif event.type == pygame.MOUSEBUTTONDOWN:
            pass
        elif event.type == pygame.MOUSEBUTTONUP:
            for button in buttons:
                if button.rect.collidepoint(event.pos):
                    index = buttons.index(button)
                    if index == 0:
                        voltage += 1
                        button_number.text = f"当前电压：{voltage}V"
                    elif index == 2:
                        voltage -= 1
                        button_number.text = f"当前电压：{voltage}V"
                    elif index == 3:
                        if voltage == 24:
                            score = 10
                            add_score(score)
                            go_next_level()
                        elif voltage > 24:
                            score = -10
                            add_score(score)
                            fire = True
                            fire_time = time.time()
                        else:
                            socre = -5
                            add_score(score)
                            go_next_level()
                        
    if fire:
        fm = pygame.transform.smoothscale(fire_image, (50, 50))
        screen.blit(fm, (350, 225))
        font = button_text_font
        text = font.render("继电器已烧毁", True, black)
        text_rect = text.get_rect(center=(screen_width // 2, 300))
        screen.blit(text, text_rect)
        if fire_time > 0 and time.time() - fire_time > 5:
            go_next_level()

    for i, button in enumerate(buttons):
        font = button_text_font
        text = font.render(button.text, True, button.font_color)
        text_rect = text.get_rect(center=button.rect.center)
        # 绘制按钮
        pygame.draw.rect(screen, button.color, button.rect)
        # 绘制文字
        screen.blit(text, text_rect)
